﻿using System;
using System.Collections.Generic;

namespace Model.Sec
{
    /// <summary>
    /// 单位类
    /// </summary>
    public class Unit
    {
        /// <summary>
        /// 单位构造函数
        /// </summary>
        public Unit()
        {
        }

        #region Unit的属性
        private string _UnitISN;
        /// <summary>
        /// 单位编码
        /// </summary>
        public string UnitISN
        {
            get { return _UnitISN; }
            set { _UnitISN = value; }
        }

        private string _UpNodeID;
        /// <summary>
        /// 上级节点编码
        /// </summary>
        public string UpNodeID
        {
            get { return _UpNodeID; }
            set { _UpNodeID = value; }
        }

        private string _UnitName;
        /// <summary>
        /// 单位名称
        /// </summary>
        public string UnitName
        {
            get { return _UnitName; }
            set { _UnitName = value; }
        }
        private DateTime _Createtime;
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime Createtime
        {
            get { return _Createtime; }
            set { _Createtime = value; }
        }
        private string _UpNodeName;
        /// <summary>
        /// 上级节点名称
        /// </summary>
        public string UpNodeName
        {
            get { return _UpNodeName; }
            set { _UpNodeName = value; }
        }
        private string _UpNodeISN;
        /// <summary>
        /// 上级节点名称
        /// </summary>
        public string UpNodeISN
        {
            get { return _UpNodeISN; }
            set { _UpNodeISN = value; }
        }
        private string _Address;
        /// <summary>
        /// 单位地址
        /// </summary>
        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        private string _Tel;
        /// <summary>
        /// 单位电话
        /// </summary>
        public string Tel
        {
            get { return _Tel; }
            set { _Tel = value; }
        }
        private string _Description;
        /// <summary>
        /// 描述
        /// </summary>
        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }
        private string _UnitSort;
        /// <summary>
        /// 单位显示顺序,同一节点下单位显示顺序
        /// </summary>
        public string UnitSort
        {
            get { return _UnitSort; }
            set { _UnitSort = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        private string _BelongSystem;
        public string BelongSystem
        {
            get { return _BelongSystem; }
            set { _BelongSystem = value; }
        }
        /// <summary>
        /// 
        /// 
        /// </summary>
        private int _NodeIndex;
        public int NodeIndex
        {
            set { _NodeIndex = value; }
            get { return _NodeIndex; }
        }
        /// <summary>
        /// 
        /// </summary>
        private string _RedirecUnit;
        public string RedirecUnit
        {
            set { _RedirecUnit = value; }
            get { return _RedirecUnit; }
        }
        /// <summary>
        /// 
        /// </summary>
        private string _RedirecUnitName;
        public string RedirecUnitName
        {
            set { _RedirecUnitName = value; }
            get { return _RedirecUnitName; }
        }
        private string _Status;
        public string Status
        {
            set { _Status = value; }
            get { return _Status; }
        }
        private string _IsMonitor;
        public string IsMonitor
        {
            set { _IsMonitor = value; }
            get { return _IsMonitor; }
        }
        private string  _SortExpression;
        /// <summary>
        /// 为了给用户排序用的
        /// </summary>
        public string SortExpression
        {
            set { _SortExpression = value; }
            get { return _SortExpression; }
        }
        private string _SortDirection;
        /// <summary>
        /// 为了给用户排序用的
        /// </summary>
        public string SortDirection
        {
            set { _SortDirection = value; }
            get { return _SortDirection; }
        }
        private UserCollection _Users;
       
        /// <summary>
        /// 单位下的用户
        /// </summary>
        public UserCollection Users
        {
            get
            {
                //if (this._Users == null)
                    this.GetUnitUsers(SortExpression,SortDirection);
                return this._Users;
            }
        }
        //private string _UnitType;
        ///// <summary>
        ///// 单位类别,自动从安全编码表中获取网管中心、台站等
        ///// </summary>
        //public string UnitType
        //{
        //    get {  return _UnitType; }
        //    set { _UnitType = value; }
        //}
        //private bool _PropertyInitialized = false;
        ///// <summary>
        ///// 是否已初始化所有属性
        ///// </summary>
        //public bool PropertyInitialized
        //{
        //    get { return this._PropertyInitialized; }
        //    set { this._PropertyInitialized = value; }
        //}
        private Node _Node;
        /// <summary>
        /// 所属节点
        /// </summary>
        public Node Node
        {
            get
            {
                if (this._Node == null)
                    this.GetNodeModel();
                return this._Node;
            }
        }
        #endregion

        #region Unit的私有方法
        /// <summary>
        /// 这个方法和OracleDAL.Sec.Node.GetModel()是一模一样的（无法直接调用后者）
        /// </summary>
        /// <param name="NodeISN"></param>
        /// <returns></returns>
        private Model.Sec.Node GetNodeModel()
        {
            Model.Sec.Node node = null;
            ///修改找到单位所属节点的语句。根据单位的unitisn
            /// yupeng 2009/3/13
            //
            string sql = "select s.*,n.* from sec_unit s,sec_node n where s.unitisn='"+this._UnitISN+"'and s.upnodeid=n.nodeid";
        
          //  string sql = "SELECT * FROM SEC_NODE WHERE NODEISN='" + this._UpNodeID + "'";
            System.Data.OracleClient.OracleDataReader odr = DBUtility.OracleHelper.ExecuteReader(sql);
            while (odr.Read())
            {
                node = new Model.Sec.Node();
                if(odr["CREATETIME"].ToString()!="")
                node.CreateTime = Convert.ToDateTime(odr["CREATETIME"]);
                node.DisplayLevel = odr["DISPLAYLEVEL"].ToString();
                node.IsCatalog = Convert.ToInt32(odr["ISCATALOG"]);
                node.LAT = float.Parse(odr["LAT"].ToString());
                node.LNG = float.Parse(odr["LNG"].ToString());
                node.NodeID = odr["NODEID"].ToString();
                node.NodeIndex = Convert.ToInt32(odr["NODEINDEX"]);
                node.NodeISN = odr["NODEISN"].ToString();
                node.NodeLevel = odr["NODELEVEL"].ToString();
                node.NodeName = odr["NODENAME"].ToString();
                node.NodeType = odr["NODETYPE"].ToString();
                node.Strip = odr["STRIP"].ToString();
                node.UpNodeID = odr["UPNODEID"].ToString();
                node.UpNodeName = odr["UPNODENAME"].ToString();
            }
            return node;
        }
        /// <summary>
        /// 单位下的用户
        /// </summary>
        /// <returns></returns>
        private void GetUnitUsers(string SortExpression, string SortDirection)
        {  
            //List<User> list = new List<User>();
            string sql = "SELECT * FROM SEC_USERINFO_new WHERE UNITISN='" + this._UnitISN + "'";
            if(SortExpression.Trim()!="")
                 sql += " order by " + SortExpression + " " + SortDirection;
            System.Data.DataTable dt = DBUtility.OracleHelper.Query(sql).Tables[0];
            UserCollection uc = new UserCollection(dt.Rows.Count);
            for (int rowNum = 0; rowNum < dt.Rows.Count; rowNum++)
            {
                Model.Sec.User user = new Model.Sec.User();
                if(dt.Rows[rowNum]["CREATETIME"].ToString()!="")
                user.CreateTime = Convert.ToDateTime(dt.Rows[rowNum]["CREATETIME"]);
                user.EMail = dt.Rows[rowNum]["EMAIL"].ToString();
                user.IPSection = dt.Rows[rowNum]["IPSECTION"].ToString();
                user.Password = dt.Rows[rowNum]["PASSWORD"].ToString();
                user.Position = dt.Rows[rowNum]["POSITION"].ToString();
                user.Sort = Convert.ToInt32(dt.Rows[rowNum]["SORT"]);
                user.Tel = dt.Rows[rowNum]["TEL"].ToString();
                user.UnitISN = dt.Rows[rowNum]["UNITISN"].ToString();
                user.UnitName = dt.Rows[rowNum]["UNITNAME"].ToString();
                user.UserISN = dt.Rows[rowNum]["USERISN"].ToString();
                user.UserName = dt.Rows[rowNum]["USERNAME"].ToString();
                uc.Users[rowNum] = user;
            }
            this._Users = uc;
        }
        #endregion
    }
}